package com.wiloon.java.network.network.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket server = new DatagramSocket(5050);
        byte[] receiveBuffer = new byte[100];
        DatagramPacket receivePacket
                = new DatagramPacket(receiveBuffer, receiveBuffer.length);
        server.receive(receivePacket);
        String receiveString = new String(receivePacket.getData(), 0, receivePacket.getLength());
        System.out.println("received str=" + receiveString);
        int port = receivePacket.getPort();
        InetAddress addr = receivePacket.getAddress();
        String sendStr = "Hello ! I'm Server";
        byte[] sendBuf;
        sendBuf = sendStr.getBytes();
        DatagramPacket sendPacket
                = new DatagramPacket(sendBuf, sendBuf.length, addr, port);
        server.send(sendPacket);
        server.close();
    }
}